package com.cn.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.pojo.User;

public class FastJsonDemo {
    public static void main(String[] args) {
        //1.将Java对象转换为Json字符串
        User user = new User();
        user.setId(1);
        user.setUsername("zhangsan");
        user.setPassword("123");
        String jsonString = JSON.toJSONString(user);
        //{"id":1,"password":"123","username":"zhangsan"}
        System.out.println(jsonString);

        //2.将JSON字符串转换为Java对象
        User u = JSON.parseObject("{\"id\":1,\"password\":\"123\",\"username\":\"zhangsan\"}", User.class);
        System.out.println(u);
    }
}
